package com.englishtest.base;

import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: xiaomin
 * To change this template use File | Settings | File Templates.
 */
public class Paginater implements Serializable {
    public static final int UNLIMITED = Integer.MAX_VALUE;
    public static final int DEFAULT_PAGESIZE = 20;

    /**
     * indicate whether total count of all data should be obtained..
     */
    private boolean needTotalCount = true;

    /**
     * the total count fetched ,should be set by DAO.<br/>
     * <B>NOTE:</B> ,This property should be set only if <code>needTotalCount</code> be set to TRUE
     */
    private int totalCount = UNLIMITED;
    /**
     * the page size
     */
    private int pageSize = DEFAULT_PAGESIZE;
    /**
     * indicate which page to be fetched,starting from 0.
     */
    private int page = 0;

    public Paginater() {
    }

    public Paginater(boolean needTotalCount) {
        this.needTotalCount = needTotalCount;
    }

    public Paginater(boolean needTotalCount, int pageSize) {
        this.needTotalCount = needTotalCount;
        this.pageSize = pageSize;
    }

    public Paginater(boolean needTotalCount, int pageSize, int page) {
        this.needTotalCount = needTotalCount;
        this.pageSize = pageSize;
        this.page = page;
    }

    /**
     * @return the specified page size
     * @see #pageSize
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * set the page size
     *
     * @param pageSize the specified page size.
     * @see #pageSize
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * @return Page index of current page fetched by DAO,starting
     */
    public int getPage() {
        return page;
    }

    /**
     * set the page index which indicate the page should be fetched.
     *
     * @param page the specified page index,starting from 1.
     */
    public void setPage(int page) {
        this.page = page;
    }
    
    public void nextPage() {
    	this.page = this.page + 1;
    }
    public void prevPage() {
    	this.page = this.page - 1;
    }


    /**
     * @return A boolean value indicating if total count of all data need be obtained.
     * @see #needTotalCount
     */
    public boolean isNeedTotalCount() {
        return needTotalCount;
    }

    /**
     * set boolean value indicating if total count of all data need be obtained.
     * and the default value will be false.
     *
     * @param needTotalCount
     */
    public void setNeedTotalCount(boolean needTotalCount) {
        this.needTotalCount = needTotalCount;
    }

    /**
     * @return the Total Count fetched,
     *         if the <code>needTotalCount</code> property didn't be set before call the appropriated DAO,this value will be
     *         {@link Integer#MAX_VALUE}
     * @see #totalCount
     */
    public int getTotalCount() {
        return totalCount;
    }

    /**
     * invoked by DAO <br/>
     * <b>Warn:</b> this method should only be called by DAO in case the total count of data is required.
     * if the invoker call this method before calling DAO,the query may failed.
     *
     * @see #totalCount
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * similar to {@link Paginater#setTotalCount(int)} and the difference is that this method accepts {@link Integer} object as parameter.
     * /**
     * invoked by DAO <br/>
     * <b>Warn:</b> this method should only be called by DAO in case the total count of data is required.
     * if the invoker call this method before calling DAO,the query may failed.
     *
     * @param count
     * @see #totalCount
     */
    public void setTotalCount(Integer count) {

        this.totalCount = null == count ? 0 : count.intValue();
    }

    /**
     * @return index of the first result
     */
    public int getFirstResult() {
        return page * pageSize;
    }

    public int getPageCount() {
        if (totalCount==UNLIMITED)
            return 0;
        // modified by xiaomin, 2005-11-24. the result of devide will be an int, then convert to float
        float nrOfPages = (float) totalCount / getPageSize();
        return (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages);
    }
    public boolean isFirstPage(){
      if (totalCount==UNLIMITED)
        return true;
        return getPage() == 0;
    }
    public boolean isLastPage(){
     if (totalCount==UNLIMITED)
        return true;
    return getPage() == getPageCount() -1;
    }
}

